#!groovy
@Library('jenkins-pipeline-lib@pj/new-bs') _

pipeline
{
  agent any
  environment
  {
    GIT_CHANGE_LOG = gitChangeLog(currentBuild.changeSets)
    //REPORT_CI_TOKEN = credentials('Report-ci-token')
  }
  stages
  {
    stage('Setup')
    {
      steps
      {
        setGithubStatus('continuous-integration/jenkins/libmemory', 'libmemory CI Build Started', 'PENDING')
        //reportciNotify("libmemory CI Build")
      }
    }
    stage('Clean')
    {
      when
      {
        expression
        {
          /*
          * If the previous build suceeeded (unstable means test failed but build passed)
          * then we continue on in CI mode. If the previous build failed we want to
          * start with a clean environment. This is done to reduce manual user interation.
          */
          return !(didLastBuildSucceed())
        }
      }
      steps
      {
        echo('Previous build failed: Running a clean build.')
        sh 'make distclean'
      }
    }
    stage('Build')
    {
      steps
      {
        sh 'make'
      }
    }
    stage('Cross compile for ARM')
    {
      steps
      {
        sh 'make CROSS=arm:cortex-m4_hardfloat BUILDRESULTS=buildresults/arm'
        sh 'ninja -C buildresults/arm/'
      }
    }
    stage('Test')
    {
      steps
      {
        sh 'make test'
      }
      post
      {
        always
        {
          // Report Tests to Jenkins
          junit 'buildresults/test/*.xml'

          // Report Tests to Report CI
          //reportciUpload('buildresults/test/*.xml','cmocka')

        }
      }
    }
    stage('Complexity')
    {
      steps
      {
        sh 'make complexity'
      }
    }
    stage('CppCheck')
    {
      steps
      {
        sh 'make cppcheck-xml'
      }
    }
    stage('Clang Analyzer')
    {
      steps
      {
        sh 'make scan-build'
      }
    }
    stage('Clang Tidy')
    {
      steps
      {
        sh 'make tidy'
      }
    }
    stage('Documentation')
    {
      steps
      {
        sh 'make docs'
      }
    }
    stage('Format')
    {
      steps
      {
        clangFormat()
      }
    }
  }
  post
  {
    success
    {
      setGithubStatus('continuous-integration/jenkins/libmemory', 'libmemory CI Build Successful', 'SUCCESS')
    }
    failure
    {
      setGithubStatus('continuous-integration/jenkins/libmemory', 'libmemory CI Build Failed', 'FAILURE')
    }
    unstable
    {
      // Unstable occurs when there are failing tests
      setGithubStatus('continuous-integration/jenkins/libmemory', 'libmemory CI build has test or warning failures', 'FAILURE')
    }
    aborted
    {
      echo "Build manually aborted on Jenkins"
      slackNotify('ABORTED')
      setGithubStatus('continuous-integration/jenkins/libmemory', 'libmemory CI Build Aborted', 'ERROR')
      //reportciCancel()
    }
    always
    {
      // Scan for open tasks, warnings, issues, etc.
      recordIssues(
        enabledForFailure: true,
        healthy: 5,
        unhealthy: 10,
        referenceJobName: 'ea-nightly/libmemory/master',
        sourceDirectory: 'buildresults',
        qualityGates: [
          // 3 new issue: unstable
          [threshold: 3, type: 'DELTA', unstable: true],
          // 5 new issues: failed build
          [threshold: 5, type: 'DELTA', unstable: false],
          // 10 total issues: unstable
          [threshold: 55, type: 'TOTAL', unstable: true],
          // 20 total issues: fail
          [threshold: 75, type: 'TOTAL', unstable: false]
        ],
        tools: [
          clang(),
          taskScanner(
            excludePattern: 'subprojects/**, buildresults/**, dependencies/**, build/**',
            includePattern: '**/*.c, **/*.cpp, **/*.h, **/*.hpp, **/*.lua, **/*.sh, **/*.build',
            normalTags: 'TODO, to do, WIP',
            highTags: 'FIXME, FIX',
            ignoreCase: true,
          ),
          cppCheck(
            pattern: 'buildresults/**/cppcheck.xml',
          ),
        ]
      )

      slackNotify(currentBuild.currentResult)
    }
  }
}
